package com.yuxi.controller;

import io.swagger.annotations.ApiOperation;
import net.rubyeye.xmemcached.MemcachedClient;
import net.rubyeye.xmemcached.exception.MemcachedException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeoutException;

/**
 * Created by yuxi on 17/1/12.
 */
@RestController
@RequestMapping("/mem")
public class MemcacheController {
    @Autowired
    MemcachedClient memcachedClient;

    /**
     * 方法test的功能描述:incr
     * 方法改动记录:
     *
     * @return java.lang.Object
     * @params : []
     * @throw
     * @version :  V1.0.0
     * @since :  17/1/12 11:18
     * <p>
     * AUTHOR   :  yuxi
     **/
    @ApiOperation("incr")
    @RequestMapping(value = "/test", method = RequestMethod.GET)
    public Object test() {
        for (int i = 0; i < 20; i++) {
            final int temp = i;
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(50);
                        if (temp == 5) {
                            memcachedClient.delete("test");
                            Thread.sleep(50);
                            System.out.println("memcache....."+memcachedClient.get("test"));
                        } else {
                            memcachedClient.incr("test", 1);
                        }
                    } catch (TimeoutException e) {
                        e.printStackTrace();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } catch (MemcachedException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }
        try {
            Thread.sleep(2000);
            Object obj = memcachedClient.get("test");
            return obj;
        } catch (TimeoutException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (MemcachedException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 方法del的功能描述:del
     * 方法改动记录:
     *
     * @return java.lang.Object
     * @params : []
     * @throw
     * @version :  V1.0.0
     * @since :  17/1/12 11:18
     * <p>
     * AUTHOR   :  yuxi
     **/
    @ApiOperation("del")
    @RequestMapping(value = "/del", method = RequestMethod.GET)
    public Object del() {
        try {
            boolean del = memcachedClient.delete("test");
            return del;
        } catch (TimeoutException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (MemcachedException e) {
            e.printStackTrace();
        }
        return null;
    }


}
